package com.jackie.tongzhuang.GognHuoShang;

import android.app.Activity;
import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.jackie.tongzhuang.Controls.GongHuoShangControl;
import com.jackie.tongzhuang.Controls.TitleBar;
import com.jackie.tongzhuang.DataBase.DbClass;
import com.jackie.tongzhuang.Models.GongHuoShang;
import com.jackie.tongzhuang.R;

import java.util.ArrayList;
import java.util.List;

public class InsertGongHuoShangActivity extends Activity implements View.OnClickListener{
    private EditText mc,dz,dh,wx;
    private LinearLayout ghslist;
    private Button back,save;
    private TitleBar titleBar;
    private DbClass dbClass = new DbClass(this);
    private List<GongHuoShang> ghss = new ArrayList<>();
    private int _ghsid = 0;
    private int _rowid = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert_gonghuoshang);
        initView();
        save.setText("新增");

        //获取供货商
        ghss = dbClass.getGongHuoShangList();
        for(int i = 0;i<ghss.size();i++){
            GongHuoShang ghs = ghss.get(i);
            GongHuoShangControl ghsc = new GongHuoShangControl(InsertGongHuoShangActivity.this,null);
            ghsc.setGongHuoShangId(ghs.getId());
            ghsc.setMingCheng(ghs.getMingCheng());
            ghsc.setDiZhi(ghs.getDiZhi());
            ghsc.setLianXiDianHua(ghs.getLianXiDianHua());
            ghsc.setRowId(i);
            ghsc.setOnClickedListener(new GongHuoShangControl.EditClickListener() {
                @Override
                public void editClick(int ghsid,int rowid) {
                    GongHuoShang ghs = dbClass.getGongHuoShangById(ghsid);
                    _rowid = rowid;
                    _ghsid = ghs.getId();
                    mc.setText(ghs.getMingCheng());
                    dz.setText(ghs.getDiZhi());
                    dh.setText(ghs.getLianXiDianHua());
                    wx.setText(ghs.getWeiXin());
                    save.setText("更新");
                }
            });
            ghslist.addView(ghsc);
        }
        titleBar.setOnClickedListener(new TitleBar.TitleBarClickListener() {
            @Override
            public void backClick(View v) {
                finish();
            }

            @Override
            public void submitClick(View v) {

            }
        });
    }

    private void initView(){
        mc = findViewById(R.id.ghs_add_mc);
        dz = findViewById(R.id.ghs_add_dz);
        dh = findViewById(R.id.ghs_add_lxdh);
        wx = findViewById(R.id.ghs_add_wx);
        back = findViewById(R.id.ghs_add_back);
        save = findViewById(R.id.ghs_add_save);
        ghslist = findViewById(R.id.ghs_add_ghslist);
        titleBar = findViewById(R.id.ghs_add_titlebar);
        back.setOnClickListener(this);
        save.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ghs_add_back:
                finish();
                break;
            case R.id.ghs_add_save:
                if(mc.getText().toString().isEmpty()){
                    Toast.makeText(InsertGongHuoShangActivity.this,"请输入供货商名称",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(dz.getText().toString().isEmpty()){
                    Toast.makeText(InsertGongHuoShangActivity.this,"请输入供货商地址",Toast.LENGTH_SHORT).show();
                    return;
                }

                if(dh.getText().toString().isEmpty()){
                    Toast.makeText(InsertGongHuoShangActivity.this,"请输入供货商联系电话",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(wx.getText().toString().isEmpty()){
                    Toast.makeText(InsertGongHuoShangActivity.this,"请输入供货商微信",Toast.LENGTH_SHORT).show();
                    return;
                }
                switch (save.getText().toString()) {
                    case "新增":
                        ContentValues values = new ContentValues();
                        values.put("MingCheng", mc.getText().toString());
                        values.put("DiZhi", dz.getText().toString());
                        values.put("LianXiDianHua", dh.getText().toString());
                        values.put("WeiXin", wx.getText().toString());
                        long i = dbClass.insert("GongHuoShang", values);
                        Toast.makeText(this, "供货商" + mc.getText() + "添加成功", Toast.LENGTH_SHORT).show();
                        GongHuoShangControl ghsc = new GongHuoShangControl(this, null);
                        ghsc.setGongHuoShangId((int) i);
                        ghsc.setMingCheng(mc.getText().toString());
                        ghsc.setDiZhi(dz.getText().toString());
                        ghsc.setLianXiDianHua((dh.getText().toString()));
                        ghslist.addView(ghsc);
                        break;

                    case "更新":
                        ContentValues editvalues = new ContentValues();
                        editvalues.put("MingCheng", mc.getText().toString());
                        editvalues.put("DiZhi", dz.getText().toString());
                        editvalues.put("LianXiDianHua", dh.getText().toString());
                        editvalues.put("WeiXin", wx.getText().toString());
                        Log.v("msgg","ffd="+_ghsid);
                        long editi = dbClass.update("GongHuoShang", editvalues,_ghsid);
                        Toast.makeText(this, "供货商" + mc.getText() + "更新成功", Toast.LENGTH_SHORT).show();
                        GongHuoShangControl _ghsc = (GongHuoShangControl) ghslist.getChildAt(_rowid);
                        _ghsc.setMingCheng(mc.getText().toString());
                        _ghsc.setDiZhi(dz.getText().toString());
                        _ghsc.setLianXiDianHua(dh.getText().toString());
                        mc.setText("");
                        dz.setText("");
                        dh.setText("");
                        wx.setText("");
                        save.setText("新增");
                        break;

                }
        }
    }
}
